package laba8;

import interfaces.task8.SerializableUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author yakimovich
 */
public class SerializationDemo {

    /**
     * @param args
     * @throws FileNotFoundException
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws ClassCastException
     */
    public static void main(String[] args) throws FileNotFoundException
             {

        SerializableUtils utils = new SerializableUtilsImpl();
        CyclicCollectionImpl collBefore = new CyclicCollectionImpl();

        CyclicItemImpl item0 = new CyclicItemImpl(1, "one");
        CyclicItemImpl item1 = new CyclicItemImpl(2, "two");
        CyclicItemImpl item2 = new CyclicItemImpl(3, "three");
       
        collBefore.add(item0);
        collBefore.add(item1);
        collBefore.add(item2);


        FileOutputStream fout = new FileOutputStream(
                "c:\\collection.ser");
        utils.serialize(fout, collBefore);

        FileInputStream fin = new FileInputStream(
                "c:\\collection.ser");
        CyclicCollectionImpl collAfter 
                = (CyclicCollectionImpl) utils.deserialize(fin);


        System.out.println("Before the serialization:");

        System.out.println("\nCyclicItem0:");
        item0 = (CyclicItemImpl) collBefore.getFirst();
        item0.showDetails();

        System.out.println("\nCyclicItem1:");
        item1 = (CyclicItemImpl) item0.nextItem();
        item1.showDetails();

        System.out.println("\nCyclicItem2:");
        item2 = (CyclicItemImpl) item1.nextItem();
        item2.showDetails();

        System.out.println("***********************");
        System.out.println("After the serialization:");

        System.out.println("\nCyclicItem0:");
        CyclicItemImpl itemAfter0 = (CyclicItemImpl) collAfter.getFirst();
        itemAfter0.showDetails();

        System.out.println("\nCyclicItem1:");
        CyclicItemImpl itemAfter1 = (CyclicItemImpl) itemAfter0.nextItem();
        itemAfter1.showDetails();

        System.out.println("\nCyclicItem2:");
        CyclicItemImpl itemAfter2 = (CyclicItemImpl) itemAfter1.nextItem();
        itemAfter2.showDetails();
        System.out.println("***********************");

        System.out.println("Equality test for CyclicCollections before and after serialization: "
                + collAfter.equals(collBefore));

    }
}
